﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using YogaDictionary.Common;
using YogaDictionary.Model;
using YogaDictionary.ViewModel;

namespace YogaDictionary.View
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        /// <summary>
        /// Initializes a new instance of the MainWindow class.
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();
            Closing += (s, e) => ViewModelLocator.Cleanup();

            // Select the text in a TextBox when it receives focus.
            EventManager.RegisterClassHandler(typeof(TextBox), PreviewMouseLeftButtonDownEvent,
                new MouseButtonEventHandler(SearchTextBox_SelectivelyIgnoreMouseButton));
            EventManager.RegisterClassHandler(typeof(TextBox), GotKeyboardFocusEvent,
                new RoutedEventHandler(SearchTextBox_SelectAllText));
            EventManager.RegisterClassHandler(typeof(TextBox), MouseDoubleClickEvent,
                new RoutedEventHandler(SearchTextBox_SelectAllText));
            EventManager.RegisterClassHandler(typeof(TextBox), KeyDownEvent,
                new KeyEventHandler(SearchTextBox_KeyDownPress));

            SearchTextBox.Focus();
        }

        private void SearchTextBox_KeyDownPress(object sender, KeyEventArgs e)
        {
            Log.Debug("Key pressed on SearchTextBox: " + e.Key);
            if (e.Key == Key.Up || e.Key == Key.Down)
            {
                var containerIndex = WordsBox.Items.IndexOf(WordsBox.SelectedItem);
                containerIndex = e.Key == Key.Up
                    ? Math.Max(0, containerIndex - 1) 
                    : Math.Min(containerIndex + 1, WordsBox.Items.Count - 1);

                WordsBox.SelectedItem = WordsBox.Items[containerIndex];
                var itemContainer = WordsBox.ItemContainerGenerator.ContainerFromIndex(containerIndex) as ListViewItem;
                Keyboard.Focus(itemContainer);
            }
        }

        private void SearchTextBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            WordsBox.ScrollIntoView(WordsBox.SelectedItem);
            e.Handled = true;
        }

        static void SearchTextBox_SelectivelyIgnoreMouseButton(object sender, MouseButtonEventArgs e)
        {
            // Find the TextBox
            DependencyObject parent = e.OriginalSource as UIElement;
            while (parent != null && !(parent is TextBox))
                parent = VisualTreeHelper.GetParent(parent);

            if (parent == null) return;
            var textBox = (TextBox)parent;
            if (textBox.IsKeyboardFocusWithin) return;
            // If the text box is not yet focused, give it the focus and
            // stop further processing of this click event.
            textBox.Focus();
            e.Handled = true;
        }

        static void SearchTextBox_SelectAllText(object sender, RoutedEventArgs e)
        {
            var textBox = e.OriginalSource as TextBox;
            if (textBox != null)
                textBox.SelectAll();
        }

    }
}